#ifndef __FORTIWTL_H__
#define __FORTIWTL_H__

typedef CSimpleArray<CString> CStringArray;

#ifndef SHGetMenu
#define SHGetMenu(hWndMB) (HMENU)SendMessage((hWndMB), \
SHCMBM_GETMENU, (WPARAM)0, (LPARAM)0);
#endif

#ifndef SHInitMaxDialog
#define SHInitMaxDialog(hWnd)	\
{ \
	SHINITDLGINFO shidi = {SHIDIM_FLAGS, hWnd, SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_DONEBUTTON};	\
	SHInitDialog(&shidi);		\
}
#endif

#define MESSAGE_VIRTUAL_HANDLER() \
{		\
	if(!::IsWindow(hWnd))	\
	return TRUE;			\
	bHandled = DefMsgHandler(hWnd, uMsg, wParam, lParam, lResult); \
	if(bHandled) \
	return TRUE; \
}

#define BEGIN_VIRTUAL_MSG_MAP(theClass) \
public: \
	virtual BOOL DefMsgHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& lResult) \
{ \
	BOOL bHandled = FALSE; \
	hWnd; \
	uMsg; \
	wParam; \
	lParam; \
	lResult; \
	bHandled; \

#define END_VIRTUAL_MSG_MAP()		\
	return FALSE; \
}

template <class T, class TBase = CWindow>
class ATL_NO_VTABLE CXDialogImpl : public CDialogImplBaseT< TBase >
{
public:
	bool m_bModal;
	HWND m_hWndParent;
	
	CXDialogImpl() : m_bModal(false), m_hWndParent(NULL) { }
	// modal dialogs
	int DoModal(HWND hWndParent = ::GetActiveWindow(), LPARAM dwInitParam = NULL)
	{
		ATLASSERT(m_hWnd == NULL);
		m_hWndParent = hWndParent;
		_Module.AddCreateWndData(&m_thunk.cd, (CDialogImplBaseT< TBase >*)this);
		m_bModal = true;
		return ::DialogBoxParam(_Module.GetResourceInstance(), MAKEINTRESOURCE(T::IDDMD),
			hWndParent, (DLGPROC)T::StartDialogProc, dwInitParam);
	}
	BOOL EndDialog(int nRetCode)
	{
		ATLASSERT(::IsWindow(m_hWnd));
		ATLASSERT(m_bModal);	// must be a modal dialog
		return ::EndDialog(m_hWnd, nRetCode);
	}
	// modeless dialogs
	HWND Create(HWND hWndParent, LPARAM dwInitParam = NULL)
	{
		ATLASSERT(m_hWnd == NULL);
		m_hWndParent = hWndParent;
		_Module.AddCreateWndData(&m_thunk.cd, (CDialogImplBaseT< TBase >*)this);
		m_bModal = false;
		HWND hWnd = ::CreateDialogParam(_Module.GetResourceInstance(), MAKEINTRESOURCE(T::IDDML),
			hWndParent, (DLGPROC)T::StartDialogProc, dwInitParam);
		ATLASSERT(m_hWnd == hWnd);
		return hWnd;
	}
	// for CComControl
	HWND Create(HWND hWndParent, RECT&, LPARAM dwInitParam = NULL)
	{
		return Create(hWndParent, dwInitParam);
	}
	BOOL DestroyWindow()
	{
		ATLASSERT(::IsWindow(m_hWnd));
		ATLASSERT(!m_bModal);	// must not be a modal dialog
		return ::DestroyWindow(m_hWnd);
	}
};

#endif	//__FORTIWTL_H__
